package com.abberbex;

import org.newdawn.fizzy.Circle;
import org.newdawn.fizzy.DynamicBody;
import org.newdawn.fizzy.Shape;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

/**
 * 
 * @author Brendan Dickerson
 *
 */
@SuppressWarnings("rawtypes")
public class Player extends DynamicBody{


	private static float height = 16;
	private static float width = 16;
	private Image image;
	private static Circle boundsR = new Circle(height);

	public Player(Shape shape, float x, float y)
	{
		super(shape, x, y);
		try {
			image = new Image("entities/player.png").getScaledCopy(32,32);
		} catch (SlickException e) {
			e.printStackTrace();
		}
		setRestitution(0);
		setFriction(0f);
		setDensity(2f);
	}
	

	public Player(float x, float y)
	{
		super(boundsR, x, y);
		try {
			image = new Image("entities/player.png").getScaledCopy(32, 32);
		} catch (SlickException e) {
			e.printStackTrace();
		}
		setRestitution(0);
		setFriction(0f);
		setDensity(2f);
	}

	public void draw(Graphics g)
	{
		image.rotate(this.getRotation());
		image.drawCentered(getX(), getY());
	}

	
	public org.newdawn.slick.geom.Circle getBounds()
	{
		org.newdawn.slick.geom.Circle c = new org.newdawn.slick.geom.Circle(getX(), getY(), width);
		return c;
	}

	public void update(Input input, int delta)
	{
		/**
		if(getXVelocity()>6.37){
			setVelocity(6.37f, getYVelocity());
		}
		if(getXVelocity()<-6.37){
			setVelocity(-6.37f, getYVelocity());
		}
		if(getYVelocity()<-6.37)
		{
			setVelocity(getXVelocity(), -6.37f);
		}
		 **/
		if(input.isKeyDown(Input.KEY_D))
		{
			if(isTouchingAnything())
			{
				applyForce(100, 0);
			}
			else
			{
				applyForce(25,0);
			}
		}
		if(input.isKeyDown(Input.KEY_A))
		{
			if(isTouchingAnything())
			{
				applyForce(-100, 0);
			}
			else
			{
				applyForce(-25,0);
			}		
		}
		if(input.isKeyPressed(Input.KEY_W)&&isTouchingAnything())
		{
			applyImpulse(0, -125);
		}
		if(!(input.isKeyDown(Input.KEY_D)&&input.isKeyDown(Input.KEY_A)))
		{
			if(isTouchingAnything()&&!(getXVelocity()==0))
			{
					if(getXVelocity()<0)
					{
						setVelocity(getXVelocity()+.1f*(-getXVelocity()), getYVelocity());
					}
					if(getXVelocity()>0)
					{
						setVelocity(getXVelocity()-.1f*getXVelocity(), getYVelocity());
					}
				}
			
		}
	}
}
